﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
using System.Data.Services.Client;

namespace FeedReader.Shared.Data
{
    public class PostsDataSource
    {
        private PostsDataServiceContext _ServiceContext = null;

        public PostsDataSource(CloudStorageAccount storageAccount)
        {
            //var storageAccount = CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
            _ServiceContext = new PostsDataServiceContext(storageAccount.TableEndpoint.ToString(), storageAccount.Credentials);

            
            // Create the tables
            // In this case, just a single table. 
            storageAccount.CreateCloudTableClient().CreateTableIfNotExist(PostsDataServiceContext.PostsTableName);
            
            
        }
        public IEnumerable<PostsDataModel> Select(string name)
        {
            var results = from c in _ServiceContext.PostsTable
                          where c.BlogID == name
                          select c;

            var query = results.AsTableServiceQuery<PostsDataModel>();
            var queryResults = query.Execute();

            return queryResults;
        }
        //public IEnumerable<PostsDataModel> SelectTitle(string name)
        //{
        //    var results = from c in _ServiceContext.PostsTable
        //                  where c.Title == name
        //                  select c;

        //    var query = results.AsTableServiceQuery<PostsDataModel>();
        //    var queryResults = query.Execute();

        //    return queryResults;
        //}
        //public IEnumerable<PostsDataModel> Select()
        //{
        //    var results = from c in _ServiceContext.PostsTable
        //                  select c;

        //    var query = results.AsTableServiceQuery<PostsDataModel>();
        //    var queryResults = query.Execute();

        //    return queryResults;
        //}
        //public IEnumerable<PostsDataModel> SelectBlobs(bool rssenabled)
        //{
        //    var results = from c in _ServiceContext.PostsTable
        //                  where c.RSSEnabled ==rssenabled
        //                  select c;

        //    var query = results.AsTableServiceQuery<PostsDataModel>();
        //    var queryResults = query.Execute();
            
        //    // this step may not be required
            
        //    return queryResults;
        //}
        //public IEnumerable<PostsDataModel> SelectRange(IEnumerable<String> items)
        //{
        //    var results = Select();

        //    var queryResults = from d in results
        //                       where items.Contains(d.BlogID)
        //                       select d;
                                
        //    // this step may not be required

        //    return queryResults;
        //}

        //public void Delete(PostsDataModel itemToDelete)
        //{
        //    _ServiceContext.AttachTo(PostsDataServiceContext.PostsTableName, itemToDelete, "*");
        //    _ServiceContext.DeleteObject(itemToDelete);
        //    _ServiceContext.SaveChanges();
        //}
        //public void DeleteRange(IEnumerable<PostsDataModel> itemsToDelete)
        //{
        //    foreach (PostsDataModel itemToDelete in itemsToDelete)
        //    {
        //        _ServiceContext.AttachTo(PostsDataServiceContext.PostsTableName, itemToDelete, "*");
        //        _ServiceContext.DeleteObject(itemToDelete);
        //        _ServiceContext.SaveChanges();
        //    }
        //}

        //public void Insert(PostsDataModel newItem)
        //{
        //    _ServiceContext.AddObject(PostsDataServiceContext.PostsTableName, newItem);
        //    _ServiceContext.SaveChanges();
        //}

        //public void Update(PostsDataModel item)
        //{
        //    _ServiceContext.AttachTo(PostsDataServiceContext.PostsTableName, item, "*");
        //    _ServiceContext.UpdateObject(item);
        //    _ServiceContext.SaveChanges();

        //}
        //public void SaveChanges()
        //{
        //    _ServiceContext.SaveChanges();
            
        //}
        //public System.Data.Services.Client.MergeOption MergeOption
        //{
        //    get
        //    {
        //        return _ServiceContext.MergeOption;
        //    }
        //    set
        //    {
        //        _ServiceContext.MergeOption = value;
        //    }
        //}

        public void Insert(PostsDataModel post)
        {
            _ServiceContext.AddObject(PostsDataServiceContext.PostsTableName, post);
            _ServiceContext.SaveChanges();
        }
    }
}
